package com.qsmx.store.core.webfile;

import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class FileWebView extends WebView {
    private FileWebChromeClient webChromeClient;

    public FileWebView(Context context, FileWebChromeClient webChromeClient) {
        super(context);
        initWebView(webChromeClient);
    }

    public FileWebView(Context context, AttributeSet attrs, FileWebChromeClient webChromeClient) {
        super(context, attrs);
        initWebView(webChromeClient);
    }

    public FileWebView(Context context, AttributeSet attrs, int defStyleAttr, FileWebChromeClient webChromeClient) {
        super(context, attrs, defStyleAttr);
        initWebView(webChromeClient);
    }

    private void initWebView(FileWebChromeClient webChromeClient) {
        this.webChromeClient = webChromeClient;
        setWebChromeClient(webChromeClient);

        WebSettings webviewSettings = getSettings();
        // 不支持缩放
        webviewSettings.setSupportZoom(false);
        // 自适应屏幕大小
        webviewSettings.setUseWideViewPort(true);
        webviewSettings.setLoadWithOverviewMode(true);
        String cacheDirPath = getContext().getFilesDir().getAbsolutePath() + "cache/";
        webviewSettings.setDomStorageEnabled(true);
        webviewSettings.setAllowFileAccess(true);
        webviewSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    }

    public void setOpenFileChooserCallBack(FileWebChromeClient.OpenFileChooserCallBack callBack) {
        webChromeClient.setOpenFileChooserCallBack(callBack);
    }
}
